package com.deft.design.state.service;

import com.deft.design.state.model.ActivityInfo;
import com.deft.design.state.model.Status;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Hide丶love
 * @date 2022/05/19
 **/
public class ActivityService {

    private static Map<String, Enum<Status>> statusMap = new ConcurrentHashMap<>();

    public static void init(String activityId, Enum<Status> status) {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.setActivityId(activityId);
        activityInfo.setActivityName("签到活动");
        activityInfo.setStatus(status);
        activityInfo.setBeginTime(LocalDateTime.now());
        activityInfo.setEndTime(LocalDateTime.now().plusDays(1));
        statusMap.put(activityId, status);
    }

    /**
     * 查询活动状态
     *
     * @param activityId 活动ID
     * @return 查询结果
     */
    public static ActivityInfo queryActivityInfo(String activityId) {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.setActivityId(activityId);
        activityInfo.setActivityName("签到活动");
        activityInfo.setStatus(statusMap.get(activityId));
        activityInfo.setBeginTime(LocalDateTime.now());
        activityInfo.setEndTime(LocalDateTime.now().plusDays(1));
        return activityInfo;
    }

    /**
     * 查询活动状态
     *
     * @param activityId 活动ID
     * @return 查询结果
     */
    public static Enum<Status> queryActivityStatus(String activityId) {
        return statusMap.get(activityId);
    }

    /**
     * 活动状态变更
     *
     * @param activityId   活动ID
     * @param beforeStatus 变更前状态
     * @param afterStatus  变更后状态
     */
    public static synchronized void execStatus(String activityId, Enum<Status> beforeStatus, Enum<Status> afterStatus) {
        if (!beforeStatus.equals(statusMap.get(activityId))) {
            return;
        }
        statusMap.put(activityId, afterStatus);
    }
}
